﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace MapEditor
{
    public class ADXInput
    {
        private KeyboardState m_kState;
        private KeyboardState m_pkState;
        private GamePadState[] m_gpState = new GamePadState[4];
        private GamePadState[] m_pgpState = new GamePadState[4];

        public ADXInput()
        {
            m_kState = Keyboard.GetState();
            m_pkState = Keyboard.GetState();
            m_gpState[0] = GamePad.GetState(PlayerIndex.One);
            m_gpState[1] = GamePad.GetState(PlayerIndex.Two);
            m_gpState[2] = GamePad.GetState(PlayerIndex.Three);
            m_gpState[3] = GamePad.GetState(PlayerIndex.Four);
            m_pgpState[0] = GamePad.GetState(PlayerIndex.One);
            m_pgpState[1] = GamePad.GetState(PlayerIndex.Two);
            m_pgpState[2] = GamePad.GetState(PlayerIndex.Three);
            m_pgpState[3] = GamePad.GetState(PlayerIndex.Four);
        }

        public void Update()
        {
            m_pkState = m_kState;
            m_kState = Keyboard.GetState();
            for (int i = 0; i < 4; i++)
            {
                m_pgpState[i] = m_gpState[i];
            }
            m_gpState[0] = GamePad.GetState(PlayerIndex.One);
            m_gpState[1] = GamePad.GetState(PlayerIndex.Two);
            m_gpState[2] = GamePad.GetState(PlayerIndex.Three);
            m_gpState[3] = GamePad.GetState(PlayerIndex.Four);
        }

        public bool GetKey(Keys k)
        {
            return m_kState.IsKeyDown(k);
        }

        public bool GetKeyPressed(Keys k)
        {
            return m_kState.IsKeyDown(k) && !m_pkState.IsKeyDown(k);
        }

        public bool GetButton(int playerIndex, Buttons b)
        {
            return m_gpState[playerIndex - 1].IsButtonDown(b);
        }

        public bool GetButtonPressed(int playerIndex, Buttons b)
        {
            return m_gpState[playerIndex - 1].IsButtonDown(b) && !m_pgpState[playerIndex - 1].IsButtonDown(b);
        }

        public bool GetButtonReleased(int playerIndex, Buttons b)
        {
            return !m_gpState[playerIndex - 1].IsButtonDown(b) && m_pgpState[playerIndex - 1].IsButtonDown(b);
        }

        public float GetLJoystickX(int playerIndex)
        {
            return m_gpState[playerIndex - 1].ThumbSticks.Left.X;
        }

        public float GetLJoystickY(int playerIndex)
        {
            return -m_gpState[playerIndex - 1].ThumbSticks.Left.Y;
        }

        public float GetRJoystickX(int playerIndex)
        {
            return m_gpState[playerIndex - 1].ThumbSticks.Right.X;
        }

        public float GetRJoystickY(int playerIndex)
        {
            return -m_gpState[playerIndex - 1].ThumbSticks.Right.Y;
        }

        public bool GetLJoystickLeft(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].ThumbSticks.Left.X < -0.75f && m_pgpState[playerIndex - 1].ThumbSticks.Left.X > -0.75f);
        }

        public bool GetLJoystickRight(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].ThumbSticks.Left.X > 0.75f && m_pgpState[playerIndex - 1].ThumbSticks.Left.X < 0.75f);
        }

        public bool GetLJoystickUp(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].ThumbSticks.Left.Y > 0.75f && m_pgpState[playerIndex - 1].ThumbSticks.Left.Y < 0.75f);
        }

        public bool GetLJoystickDown(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].ThumbSticks.Left.Y < -0.75f && m_pgpState[playerIndex - 1].ThumbSticks.Left.Y > -0.75f);
        }

        public float GetLTrigger(int playerIndex)
        {
            return m_gpState[playerIndex - 1].Triggers.Left;
        }

        public bool GetLTriggerPressed(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].Triggers.Left > 0.75f && m_pgpState[playerIndex - 1].Triggers.Left < 0.75f);
        }

        public float GetRTrigger(int playerIndex)
        {
            return m_gpState[playerIndex - 1].Triggers.Right;
        }

        public bool GetRTriggerPressed(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].Triggers.Right > 0.75f && m_pgpState[playerIndex - 1].Triggers.Right < 0.75f);
        }

        public bool GetRTriggerReleased(int playerIndex)
        {
            return (m_gpState[playerIndex - 1].Triggers.Right < 0.75f && m_pgpState[playerIndex - 1].Triggers.Right > 0.75f);
        }
    }
}
